/**
 * $Revision$
 * $Date$
 *
 * This file is part of the Solveur repository software.
 * Copyright (C) 2012 Solveur developer team
 * See http://code.google.com/p/solveur/
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 **/

package gougeon.android.solveur;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.Spinner;

public class SolveurActivity extends Activity {

	public static final String mimeType = "text/html";
	public static final String encoding = "utf-8";
	private final String html = "";
	public static String css_string;
	
	/** Called when the activity is first created. */
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.main);
		final Spinner mon_spinner1 = (Spinner) findViewById(R.id.spinner1);
		final Spinner mon_spinner2 = (Spinner) findViewById(R.id.spinner2);
		mon_spinner1.setSelection(0);
		mon_spinner2.setSelection(1);
		final EditText mon_edittext = (EditText) findViewById(R.id.input_text);
		final WebView wv = (WebView) findViewById(R.id.webView1);
		wv.loadDataWithBaseURL("fake://not/needed", html, mimeType, encoding, "");
		css_string=this.getString(R.string.css_head);

		InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		// only will trigger it if no physical keyboard is open
		mgr.showSoftInput(mon_edittext, InputMethodManager.SHOW_IMPLICIT);

		handler	= new MonHandler(this);

		mon_edittext.setOnKeyListener(new OnKeyListener() {
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// If the event is a key-down event on the "enter" button
				if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
						(keyCode == KeyEvent.KEYCODE_ENTER)) 
				{
					// Perform action on key press
					mon_thread(mon_edittext.getText().toString().toUpperCase());
					return true;
				}
				return false;
			}
		});

		mon_edittext.addTextChangedListener(new TextWatcher(){
	        public void afterTextChanged(Editable s) {
	        	int longueur=s.length();
	        	int longueur2;
	        	if (longueur<2)
	        	{
	        		longueur=2;
	        	}
	        	else if (longueur>7)
	        	{
	        		longueur=7;
	        	}
	        	longueur2=longueur-3;
	        	if (longueur2<0)
	        	{
					longueur2=0;
	        	}
	    		mon_spinner1.setSelection(longueur2);
	    		mon_spinner2.setSelection(longueur-1);
	        }
	        public void beforeTextChanged(CharSequence s, int start, int count, int after){}
	        public void onTextChanged(CharSequence s, int start, int before, int count){}
	    });
	}

	private void mon_thread(final String analyse_chaine) {
		final MonThread monThread = new MonThread(this, handler, analyse_chaine);
		new Thread(monThread).start();
	}

	private Handler handler;

};